package yxy.game.pm2.module.kow;

import java.util.ArrayList;
import java.util.List;

import org.joda.time.DateTime;
import org.redisson.api.RList;

import yxy.apple.util.redisson.RedisException;
import yxy.cherry.battle.module.record.result.BattleResult;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.game.pm2.bean.db.newDao.KowRecordDAO;
import yxy.game.pm2.bean.db.po.KowRecordPo;
import yxy.game.pm2.bean.module.PlayerModule;

public class KowRecord extends PlayerModule {
    // bean===========================================================================
    private DateTime date;// 时间
    private String recordCode;// 录像code

    private String attackerCode;// 进攻方
    private String defenderCode;// 防守方

    private int scoreChangeAttacker;// 进攻方分数变化
    private int scoreChangeDefender;// 防守方分数变化

    private BattleResult battleResult;


    public DateTime getDate() {
        return date;
    }

    public void setDate(DateTime date) {
        this.date = date;
    }

    public String getRecordCode() {
        return recordCode;
    }

    public void setRecordCode(String recordCode) {
        this.recordCode = recordCode;
    }

    public String getAttackerCode() {
        return attackerCode;
    }

    public void setAttackerCode(String attackerCode) {
        this.attackerCode = attackerCode;
    }

    public String getDefenderCode() {
        return defenderCode;
    }

    public void setDefenderCode(String defenderCode) {
        this.defenderCode = defenderCode;
    }

    public int getScoreChangeAttacker() {
        return scoreChangeAttacker;
    }

    public void setScoreChangeAttacker(int scoreChangeAttacker) {
        this.scoreChangeAttacker = scoreChangeAttacker;
    }

    public int getScoreChangeDefender() {
        return scoreChangeDefender;
    }

    public void setScoreChangeDefender(int scoreChangeDefender) {
        this.scoreChangeDefender = scoreChangeDefender;
    }

    public BattleResult getBattleResult() {
        return battleResult;
    }

    public void setBattleResult(BattleResult battleResult) {
        this.battleResult = battleResult;
    }

    // func===========================================================================
    // redis==========================================================================

    static public String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:KOWRECORD", playerCode);
    }

    static private RList<KowRecord> touch(String playerCode) throws RedisException {
        return RedisManager.client(redis()).getList(key(playerCode));
    }

    static public List<KowRecord> get(String playerCode) {
        try {
            List<KowRecord> list = touch(playerCode).readAll();
            //缓存没数据从数据库读取并放到redis
//            System.out.println("KowRecordFromRedis:" + list);
//            if (list.size() <= 0) {
//                List<KowRecordPo> poList = KowRecordDAO.getList(playerCode);
//                System.out.println("KowRecordFromTable:" + poList);
//                for (KowRecordPo po : poList) {
//                    KowRecord kowRecord = toBean(po, playerCode);
//                    list.add(kowRecord);
//                }
//
//                if (list.size() > 0) {
//                    //将数据放到缓存
//                    touch(playerCode).addAll(list);
//                }
//            }
            return list;
        } catch (RedisException e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    static public void add(String playerCode, KowRecord record) {
        try {
            RList<KowRecord> rlist = touch(playerCode);
            rlist.add(0, record);
            if (rlist.size() > 10) {
                rlist.trim(0, 9);
            }
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            System.out.println("KowRecord执行saveToDB");
            RList<KowRecord> rList = touch(playerCode);
            System.out.println("rList.isExists()：" + rList.isExists());
            if (rList.isExists()) {
                long updateTime = System.currentTimeMillis();
                //存储
                List<KowRecordPo> kowRecordPoList = new ArrayList<>();
                List<KowRecord> kowRecordList = rList.readAll();
                for (KowRecord kowRecord : kowRecordList) {
                    KowRecordPo po = toDbBean(kowRecord, playerCode, updateTime);
                    kowRecordPoList.add(po);
                }
                System.out.println("kowRecordPoList：" + kowRecordPoList);
                //保存数据库
                KowRecordDAO.replace(kowRecordPoList);
                //
                KowRecordDAO.delete(playerCode, updateTime);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //将bean转po
    private static KowRecordPo toDbBean(KowRecord kowRecord, String playerCode, long updateTime) {
        KowRecordPo kowRecordPo = new KowRecordPo();
        kowRecordPo.setPlayerCode(playerCode);
        kowRecordPo.setDate(kowRecord.getDate().toDate());
        kowRecordPo.setRecordCode(kowRecord.getRecordCode());
        kowRecordPo.setAttackerCode(kowRecord.getAttackerCode());
        kowRecordPo.setDefenderCode(kowRecord.getDefenderCode());
        kowRecordPo.setScoreChangeAttacker(kowRecord.getScoreChangeAttacker());
        kowRecordPo.setScoreChangeDefender(kowRecord.getScoreChangeDefender());
        kowRecordPo.setBattleResult(kowRecord.getBattleResult().toString());

        return kowRecordPo;
    }

    //将po转回bean
    private static KowRecord toBean(KowRecordPo po, String playerCode) {
        KowRecord kowRecord = new KowRecord();
        kowRecord.setPlayerCode(playerCode);
        kowRecord.setDate(new DateTime(po.getDate()));
        kowRecord.setRecordCode(po.getRecordCode());
        kowRecord.setAttackerCode(po.getAttackerCode());
        kowRecord.setDefenderCode(po.getDefenderCode());
        kowRecord.setScoreChangeAttacker(po.getScoreChangeAttacker());
        kowRecord.setScoreChangeDefender(po.getScoreChangeDefender());
        kowRecord.setBattleResult(BattleResult.battleResult(po.getBattleResult()));
        return kowRecord;
    }

    //清楚缓存
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (RedisException e) {
            e.printStackTrace();
        }
    }
    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }
}
